package user

import (
	"context"
	"fmt"
	"github.com/google/uuid"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	//"github.com/gogf/gf-demo-user/v2/internal/model"
	"github.com/gogf/gf-demo-user/v2/internal/model/entity"
	"github.com/gogf/gf-demo-user/v2/api/web/v1"
	"github.com/gogf/gf-demo-user/v2/internal/service"
)

func (c *ControllerV1) Company(ctx context.Context, req *v1.CompanyReq) (res *v1.CompanyRes, err error) {
	res = &v1.CompanyRes{
		service.User().GetCompany(ctx,req.Company_id),
	}
	return
}

func (c *ControllerV1) CompanyPost(ctx context.Context, req *v1.CompanyPostReq) (res *v1.CompanyPostRes, err error) {
	err = service.User().CompanyPost(ctx, req)
	return
}

func (c *ControllerV1) CompanyUp(ctx context.Context, req *v1.CompanyUpReq) (res *v1.CompanyUpRes, err error) {
	err = service.User().CompanyUp(ctx, req)
	return
}

func (c *ControllerV1) CompanyMe(ctx context.Context, req *v1.CompanyMeReq) (res *v1.CompanyMeRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var company *entity.Company
	err = g.Model("Company").Where("id=?", account.Id).Scan(&company)
	fmt.Println(err)
	res = &v1.CompanyMeRes{
		company,
	}
	return
}

func (c *ControllerV1) CompanyMeUp(ctx context.Context, req *v1.CompanyMeUpReq) (res *v1.CompanyRes, err error) {
	err = service.User().CompanyMeUp(ctx, req)
	return
}

func (c *ControllerV1) CompanyMeDele(ctx context.Context, req *v1.CompanyMeDeleReq) (res *v1.CompanyMeDeleRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("CompanySubscription").Where("company_id=?", account.Id).Delete()
	fmt.Println(id)
	return
}

func (c *ControllerV1) CompanySubscription(ctx context.Context, req *v1.CompanySubscriptionReq) (res *v1.CompanySubscriptionRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	var user *entity.Subscription
	err = g.Model("CompanySubscription").Where("company_id=?", account.Id).Scan(&user)
	fmt.Println(err)
	res = &v1.CompanySubscriptionRes{
		user,
	}
	return
}

func (c *ControllerV1) CompanySubscriptionUp(ctx context.Context, req *v1.CompanySubscriptionUpReq) (res *v1.CompanySubscriptionUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	id, err := g.Model("CompanySubscription").Data(g.Map{
		"company_plan" : req.Company_plan,
		"status" : req.Status,
		"expiry" : req.Expiry,
	}).Where("id", account.Id).Update()
	fmt.Println(id)
	return
}

func (c *ControllerV1) CompanySubscriptionPost(ctx context.Context, req *v1.CompanySubscriptionPostReq) (res *v1.CompanySubscriptionUpRes, err error) {
	var account *entity.Account
	account = service.Session().GetUser(ctx)
	fmt.Println(account)
	if account == nil {
		return nil, gerror.New(`Please login first`)
	}
	newUUID := uuid.New()
	id, err := g.Model("CompanySubscription").Data(g.Map{
		"id" : newUUID,
		"company_id" : account.Id,
		"company_plan" : req.Company_plan,
		"status" : req.Status,
		"expiry" : req.Expiry,
	}).Insert()
	fmt.Println(id)
	return
}




